﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace PlutoStudio.Data.Configuration
{
    public class ConnectionPoolSection : ConfigurationSection
    {
        [ConfigurationProperty("provider")]
        public string SqlClientProvider
        {
            get
            {
                return base["provider"].ToString();
            }
        }

        [ConfigurationProperty("",IsDefaultCollection=true)]
        public ConnectionCollection Connections
        {
            get
            {
                return (ConnectionCollection)base[""];
            }
        }
    }

    public class ConnectionCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ConnectionElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ConnectionElement)element).Id;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        protected override string ElementName
        {
            get
            {
                return "connection";
            }
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            base.BaseAdd(element,false);
        }

        public ConnectionElement this[int id]
        {
            get
            {
                return (ConnectionElement)BaseGet((object)(id));
            }
        }
    }

    public class ConnectionElement : ConfigurationElement
    {
        [ConfigurationProperty("id")]
        public int Id { get { return Convert.ToInt32(base["id"]); } }
        
        [ConfigurationProperty("connectionString")]
        public string ConnectionString { get { return base["connectionString"].ToString(); } }
    }
}
